package GUI;

import DAO.TreatmentDao;

import Logica.Treatment1;

import java.awt.Color;
import java.awt.Dimension;

import java.awt.Font;
import java.awt.Rectangle;

import java.awt.SystemColor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

public class Tratamiento extends JFrame {
  private JLabel jlabelTratamiento = new JLabel();
  private JButton jbuttonAgregarT = new JButton();
  AgregarT1 nuevo = new AgregarT1();
  private JButton jBModificarTrata = new JButton();
  Treatment1 tre = new Treatment1();
  private JTable jTableTreatment = new JTable();
  private JButton jbEliminar = new JButton();


  public Tratamiento() {
    try {
      jbInit();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.getContentPane().setLayout(null);
    this.setSize(new Dimension(804, 569));
    this.setForeground(Color.white);
    this.setTitle("TRATAMIENTOS");
    this.setBackground(Color.white);
    jlabelTratamiento.setText("TRATAMIENTOS");
    jlabelTratamiento.setBounds(new Rectangle(260, 10, 180, 55));
    jlabelTratamiento.setFont(new Font("ALGERIAN", 0, 22));
    jlabelTratamiento.setForeground(new Color(248, 14, 30));
    jbuttonAgregarT.setText("AGREGAR ");
    jbuttonAgregarT.setBounds(new Rectangle(235, 475, 150, 45));
    jbuttonAgregarT.setFont(new Font("ARIAL BLACK", 0, 14));
    jbuttonAgregarT.setForeground(new Color(0, 245, 245));
    jbuttonAgregarT.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jbuttonAgregarT_actionPerformed(e);
        }
      });

    jBModificarTrata.setText("MODIFICAR");
    jBModificarTrata.setBounds(new Rectangle(400, 475, 145, 45));
    jBModificarTrata.setFont(new Font("arial black", 0, 14));
    jBModificarTrata.setForeground(new Color(0, 245, 245));

    //tabla

    jBModificarTrata.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          jBModificarTrata_actionPerformed(e);
        }
      });

    Object[][] info = tre.DarDatosGuiTratamiento();
    String[] tit =
    { "TREATMENT NUMBER", "DESCRIPTION", "TCOST", "QUANTITY", "OBSERVATION",
      "BEGINDATE", "END DATE", "EXAMINATION NUMBER" };
    DefaultTableModel dtm = new DefaultTableModel(info, tit);

    jTableTreatment = new JTable(dtm);

    jTableTreatment.setPreferredScrollableViewportSize(new Dimension(300,
                                                                     100)); // las dimensiones
    jTableTreatment.setBounds(new Rectangle(10, 105, 690, 350));
    jTableTreatment.setSize(new Dimension(750, 350));
    jbEliminar.setText("ELIMINAR");
    jbEliminar.setBounds(new Rectangle(560, 475, 145, 45));
    jbEliminar.setFont(new Font("arial black", 0, 14));
    jbEliminar.setForeground(new Color(0, 245, 245));
    jbEliminar.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          EliminarTratamiento(e);
        }
      });
    
    this.getContentPane().add(jbEliminar, null);
    this.getContentPane().add(jBModificarTrata, null);
    this.getContentPane().add(jbuttonAgregarT, null);
    this.getContentPane().add(jlabelTratamiento, null);
    this.getContentPane().add(jTableTreatment, null);

  }

  private void jbuttonAgregarT_actionPerformed(ActionEvent e) {
    //LLAMAR A AGREGAR TRATAMIENTO
    nuevo.setVisible(true);
  }

  private void jBModificarTrata_actionPerformed(ActionEvent e) {
    int fila = jTableTreatment.getSelectedRow();
    System.out.println(fila);
    Object[] info = new Object[8];
    for (int i = 0; i < jTableTreatment.getColumnCount(); i++){
      info[i] = jTableTreatment.getValueAt(fila, i);
    }
    nuevo.setVisible(false);
    ModificarTratamiento1 modN = new ModificarTratamiento1(info);
    modN.setVisible(true);
  }

  private void EliminarTratamiento(ActionEvent e) {
    int fila = jTableTreatment.getSelectedRow();
 
    Object[] info = new Object[8];
    for (int i = 0; i < jTableTreatment.getColumnCount(); i++){
      info[i] = jTableTreatment.getValueAt(fila, i);
    }
    System.out.println(info[0]);
    nuevo.setVisible(false);
    if(tre.EliminarTratamiento(String.valueOf(info[0])))
    {
      JOptionPane.showMessageDialog(null, "Eliminado", "El tratamiento ha sido eliminado", JOptionPane.INFORMATION_MESSAGE); 
      }
    else{
        JOptionPane.showMessageDialog(null, "No Eliminado",
                                      "El tratamiento no ha sido Eliminado",
                                      JOptionPane.ERROR_MESSAGE);
      }
     
  }
}
